package com.hczt.xhminiapp.appapi.weixin.controller;

import com.hczt.xhminiapp.appapi.weixin.service.WxService;
import com.hczt.xhminiapp.common.bean.CodeMsg;
import com.hczt.xhminiapp.common.bean.RtnResult;
import com.hczt.xhminiapp.common.exception.BizException;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 红创-马海强
 * @date 2019-03-07 16:35
 * @description
 */
@Slf4j
@RestController
@Api(tags="微信接口")
public class WxController {

    @Autowired
    private WxService wxService;

    /**
     * 微信公众号用户授权
     * @param code
     * @return
     * @throws BizException
     */
    @GetMapping("/public/wx/authorized/{code}")
    public RtnResult<Object> wxWebAuthorizedLogin(HttpServletRequest request, @PathVariable String code) throws BizException {

        try {
            String role = request.getHeader("role");
            log.info("微信公众号用户授权，role为【{}】,code为【{}】", role, code);
            return wxService.getUserInfoByCode(role, code);
        } catch (Exception e) {
            log.error("获取微信网页授权发生异常,异常信息为", e);
            throw new BizException(CodeMsg.SERVER_ERROR);
        }
    }
}
